<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input style="width: 150px;" v-model="dataForm.name" placeholder="受助者姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select style="width: 150px;margin-left: 20px" v-model="dataForm.categoryId" clearable placeholder="受助类别">
          <el-option v-for="item in categorys" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 110px;" v-model="dataForm.startAge" placeholder="开始年龄" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 110px;" v-model="dataForm.endAge" placeholder="结束年龄" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select style="width: 110px;margin-left: 20px" v-model="dataForm.nurseGrade" clearable placeholder="护理类别">
          <el-option v-for="item in nurseType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 150px;" v-model="dataForm.address" placeholder="居住地址" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select style="width: 110px;margin-left: 20px" v-model="dataForm.isLove" clearable placeholder="精神关爱">
          <el-option v-for="item in needType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select style="width: 110px;margin-left: 20px" v-model="dataForm.isMaterial" clearable placeholder="物资关爱">
          <el-option v-for="item in needType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 100px;" v-model="dataForm.fellowName" placeholder="对接人" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAdmin" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column prop="name" header-align="center" align="center" label="姓名" width="180px">
      </el-table-column>
      <el-table-column prop="identityId" header-align="center" align="center" label="身份证号">
      </el-table-column>
      <el-table-column prop="gender" header-align="center" align="center" label="性别">
        <template v-slot:default="scope">
          {{ scope.row.gender === 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="birthDate" header-align="center" align="center" label="出生日期">
      </el-table-column>
      <el-table-column prop="isLove" header-align="center" align="center" label="精神关爱">
        <template slot-scope="scope">
          <span v-if="scope.row.isLove === 0">不需要</span>
          <span v-else-if="scope.row.isLove === 1">需要</span>
          <span v-else>待观察</span>
        </template>
      </el-table-column>
      <el-table-column prop="isMaterial" header-align="center" align="center" label="物资关爱">
        <template slot-scope="scope">
          <span v-if="scope.row.isMaterial === 0">不需要</span>
          <span v-else-if="scope.row.isMaterial === 1">需要</span>
          <span v-else>待观察</span>
        </template>
      </el-table-column>
      <el-table-column prop="origin" header-align="center" align="center" label="籍贯">
      </el-table-column>
      <el-table-column prop="nurseName" header-align="center" align="center" label="护理人姓名">
      </el-table-column>
      <el-table-column prop="nurseTel" header-align="center" align="center" label="护理人电话">
      </el-table-column>
      <el-table-column prop="nurseGrade" header-align="center" align="center" label="护理类别">
        <template slot-scope="scope">
          <span v-if="scope.row.nurseGrade === 0">全自理</span>
          <span v-else-if="scope.row.nurseGrade === 1">半失能</span>
          <span v-else-if="scope.row.nurseGrade === 2">失能</span>
          <span v-else>正常</span>
        </template>
      </el-table-column>
      <el-table-column prop="coolDetail" header-align="center" align="center" label="取凉情况">
      </el-table-column>
      <el-table-column prop="warmDetail" header-align="center" align="center" label="取暖情况">
      </el-table-column>
      <el-table-column prop="age" header-align="center" align="center" label="年龄">
      </el-table-column>
      <el-table-column prop="hygieneDetail" header-align="center" align="center" label="卫生情况">
      </el-table-column>
      <el-table-column prop="economicSource" header-align="center" align="center" label="经济来源">
      </el-table-column>
      <el-table-column prop="resideDetail" header-align="center" align="center" label="居住生活情况">
      </el-table-column>
      <el-table-column prop="medicationDetail" header-align="center" align="center" label="服药情况">
      </el-table-column>
      <el-table-column prop="healthyDetail" header-align="center" align="center" label="健康情况">
      </el-table-column>
      <el-table-column prop="address" header-align="center" align="center" label="居住地址">
      </el-table-column>
      <el-table-column prop="neighborDetail" header-align="center" align="center" label="邻里关系">
      </el-table-column>
      <el-table-column prop="tel" header-align="center" align="center" label="本人电话">
      </el-table-column>
      <el-table-column prop="reamrk" header-align="center" align="center" label="备注">
      </el-table-column>
      <el-table-column prop="villageName" header-align="center" align="center" label="乡干部姓名">
      </el-table-column>
      <el-table-column prop="villageTel" header-align="center" align="center" label="乡干部电话">
      </el-table-column>
      <el-table-column prop="townName" header-align="center" align="center" label="镇干部姓名">
      </el-table-column>
      <el-table-column prop="townTel" header-align="center" align="center" label="镇干部电话">
      </el-table-column>
      <el-table-column prop="fellowName" header-align="center" align="center" label="对接人">
      </el-table-column>
      <el-table-column prop="fellowTel" header-align="center" align="center" label="对接人电话">
      </el-table-column>
      <el-table-column prop="categoryName" header-align="center" align="center" label="类别">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="primary" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './recipient-add-or-update'
import request from '@/utils/httpRequest'
import store from '@/store'
export default {

  data() {
    return {
      genderType: [
        {
          key: 0,
          value: '男'
        },
        {
          key: 1,
          value: '女'
        }
      ],
      nurseType: [
        {
          key: 0,
          value: '全自理'
        },
        {
          key: 1,
          value: '半失能'
        },
        {
          key: 2,
          value: '失能'
        },
        {
          key: 3,
          value: '正常'
        }
      ],
      needType: [
        {
          key: 0,
          value: '不需要'
        },
        {
          key: 1,
          value: '需要'
        },
        {
          key: 2,
          value: '待观察'
        },
      ],
      categorys: [
        {
          id: 0,
          name: ''
        }
      ],
      dataForm: {
        name: '',
        categoryId: '',
        startAge: '',
        endAge: '',
        nurseGrade: '',
        address: '',
        isLove: '',
        isMaterial: '',
        fellowName: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.getDataList()
  },

  methods: {
    getCategorys() {
      request.get('/lovehelp/category/allcategory').then((response) => {
        if (response && response.code === 200) {
          this.categorys = response.categorys
        } else {
          this.categorys = []
        }
      }).catch((error) => {
        console.error(error)
      })
    },
    // 获取数据列表
    getDataList() {
      this.getCategorys()
      this.dataListLoading = true
      request.get(`/lovehelp/recipient/list`, {
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'name': this.dataForm.name,
          'categoryId': this.dataForm.categoryId,
          'startAge': this.dataForm.startAge,
          'endAge': this.dataForm.endAge,
          'nurseGrade': this.dataForm.nurseGrade,
          'address': this.dataForm.address,
          'isLove': this.dataForm.isLove,
          'isMaterial': this.dataForm.isMaterial,
          'fellowName': this.dataForm.fellowName,
        }
      }).then((response) => {
        if (response && response.code === 200) {
          this.dataList = response.page.list;
          this.totalPage = response.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      }).catch((error) => {
        // 处理错误  
        console.error(error);
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request.post('/lovehelp/recipient/delete', ids)
          .then(response => {
            if (response.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(response.data.msg);
            }
          })
          .catch(error => {
            // 处理请求错误  
            console.error('请求失败:', error);
            this.$message.error('请求失败，请稍后再试');
          });
      }).catch((error) => {
        // 处理错误  
        console.error(error);
      });
    }
  },
  computed: {
    isAdmin() {
      return this.$store.state.user.loginUser.role===1 || store.state.user.loginUser.role===2
    }
  }
}
</script>
